-- UIBrokenPC
-- Created by cheny3 Oct/14/2016
-- 破旧的电脑

UIBrokenPC = class("UIBrokenPC", function()
    return cc.Layer:create();
end);

function UIBrokenPC.create(gridData)
    return UIBrokenPC.new(gridData);
end

-- 构造函数
function UIBrokenPC:ctor(gridData)
    self:setName("UIBrokenPC");

    -- 复用座钟界面
    local node = cc.CSLoader:createNode("layout/dungeon/special/DeskClock.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBrokenPC:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBrokenPC:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBrokenPC 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UIBrokenPC");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBrokenPC:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBrokenPC");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 第1个按钮效果
    local btnNode = findChildByName(node, "CT/btn_node");
    local btn1 = btnNode:getChildByName("btn_hour");
    local function onFirstClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            self:takeBrokenPcBonus();
        end
    end
    AddTouchEventListener(btn1, onFirstClick);
    TextStyleM.setTextStyle(btn1, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btn1:setTitleText(getLocStr("btn_text_pc1"));

    -- 第2个按钮效果
    local btn2 = btnNode:getChildByName("btn_minute");
    local function onSecondClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            self:takeBrokenPcBonus();
        end
    end
    AddTouchEventListener(btn2, onSecondClick);
    TextStyleM.setTextStyle(btn2, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btn2:setTitleText(getLocStr("btn_text_pc2"));

    -- 第3个按钮效果
    local btn3 = btnNode:getChildByName("btn_open");
    local function onThirdClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            self:takeBrokenPcBonus();
        end
    end
    AddTouchEventListener(btn3, onThirdClick);
    TextStyleM.setTextStyle(btn3, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btn3:setTitleText(getLocStr("btn_text_pc3"));
end

-- 领取奖励
function UIBrokenPC:takeBrokenPcBonus()
    local pos = self.gridData:getPos();
    DungeonActionM.go("take_broken_pc_bonus", pos);
    UIMgr.getCurrentScene():removeFormByName("UIBrokenPC");
end

-- 显隐绘制节点
function UIBrokenPC:showNodes()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local timeLabel  = findChildByName(self.node, "CT/time_label");
    local targetNode = findChildByName(self.node, "CT/target_node");
    local itemNode   = findChildByName(self.node, "CT/item_node");
    local item2Node  = findChildByName(self.node, "CT/item2_node");
    local btn1       = findChildByName(self.node, "CT/btn_node/btn_hour");
    local btn2       = findChildByName(self.node, "CT/btn_node/btn_minute");
    local btn3       = findChildByName(self.node, "CT/btn_node/btn_open");
    local icon1      = findChildByName(btn1, "icon");
    local icon2      = findChildByName(btn2, "icon");

    -- 升级部分
    targetNode:setVisible(false);
    itemNode:setVisible(false);
    item2Node:setVisible(false);

    -- 描述部分
    iconImg:setVisible(true);
    descLabel:setVisible(true);
    timeLabel:setVisible(false);

    -- 按钮部分
    btn1:setVisible(true);
    btn2:setVisible(true);
    btn3:setVisible(true);
    icon1:setVisible(false);
    icon2:setVisible(false);
end

-- 主界面
function UIBrokenPC:redrawMain()
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local btn1       = findChildByName(self.node, "CT/btn_node/btn_hour");
    local btn2       = findChildByName(self.node, "CT/btn_node/btn_minute");
    local btn3       = findChildByName(self.node, "CT/btn_node/btn_open");

    -- 建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    titleLabel:setString(buildingInfo["title"]);
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 图片
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    iconImg:setScale(1.3);
    iconImg:setOpacity(255);

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    -- 描述
    local dialog = buildingInfo["dialog"];
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    descLabel = applyString(descLabel, dialog);

    -- 按钮
    btn1:setTitleText(getLocStr("btn_text_pc1"));
    btn2:setTitleText(getLocStr("btn_text_pc2"));
    btn3:setTitleText(getLocStr("btn_text_pc3"));
end

-- 重绘
function UIBrokenPC:redraw()
    -- 显隐节点处理
    self:showNodes();

    -- 主界面
    self:redrawMain();
end